package com.wavehi.consumer;

import com.rabbitmq.client.CancelCallback;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.DeliverCallback;
import com.wavehi.util.RabbitMQFactoryUtils;

/**
 * @Description 消费者 演示扇出交换机的使用
 * @Author WaveHi
 * @Date 27/12/2021 下午 7:33
 * @Version 1.0
 * @Contact HJBWaveHi@163.com
 */
public class Consumer_05 {
    //交换机名词
    private static final String EXCHANGE_NAME = "log_exchange";
    public static void main(String[] args) throws Exception {
        //1.获取连接信道
        Channel channel = RabbitMQFactoryUtils.getChannel();
        //2.声明交换机
        channel.exchangeDeclare(EXCHANGE_NAME, "fanout");
        //3.声明队列
        String queue = channel.queueDeclare().getQueue();
        //4.绑定交换机与队列
        channel.queueBind(queue, EXCHANGE_NAME, "Others");
        //5.接收消息
        //5.1.准备消费操作
        DeliverCallback deliverCallback = (consumerTag, message) -> {
            System.out.println("Accept message " + new String(message.getBody(), "UTF-8"));
        };
        //5.2.准备消费中止后的操作
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("Message send failed !");
        };
        //5.3.接收
        channel.basicConsume(queue, true, deliverCallback, cancelCallback);
    }
}
